package cn.steven.player.control

import android.content.Context
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.LoadEventInfo
import com.google.android.exoplayer2.source.MediaLoadData
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.MediaSourceEventListener


class EasyPlayer(context: Context) {
    private lateinit var mediaSource: MediaSource
    private val TAG = "EasyPlayer"
    private val workHandler: Handler
    private var context: Context
    private lateinit var mMediaSourceHelper: ExoMediaSourceHelper
    private lateinit var mediaSourceEventListener : MediaSourceEventListener
    private lateinit var corePlayer: ExoPlayer


    init {
        this.context = context;
        val handlerThread = HandlerThread(TAG)
        handlerThread.start()
        workHandler = Handler(handlerThread.looper)
//        workHandler = Handler(Looper.getMainLooper())
        workHandler.post {
            corePlayer = ExoPlayer.Builder(context).build()
            corePlayer.addListener(createListener())
            mMediaSourceHelper = ExoMediaSourceHelper(context)
            mediaSourceEventListener = createMediaSourceEventListener()
        }
    }

    private fun createMediaSourceEventListener(): MediaSourceEventListener {
        return object : MediaSourceEventListener{
            override fun onLoadCompleted(
                windowIndex: Int,
                mediaPeriodId: MediaSource.MediaPeriodId?,
                loadEventInfo: LoadEventInfo,
                mediaLoadData: MediaLoadData
            ) {
                Log.d(TAG, "onLoadCompleted")
            }

            override fun onLoadStarted(
                windowIndex: Int,
                mediaPeriodId: MediaSource.MediaPeriodId?,
                loadEventInfo: LoadEventInfo,
                mediaLoadData: MediaLoadData
            ) {
                Log.d(TAG, "onLoadStarted")
            }
        }
    }

    private fun createListener(): Player.Listener {
        return object : Player.Listener {
            override fun onPlaybackStateChanged(state: Int) {
                super.onPlaybackStateChanged(state)
                Log.d(TAG, "onPlaybackStateChanged: $state")
                when (state) {
                    Player.STATE_IDLE -> {

                    }
                }
            }
        }
    }

    fun playByUrl(url: String) {
        Log.i(TAG,"playByUrl: $url")
        workHandler.post {
//            val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
//            val hlsMediaSource: HlsMediaSource = HlsMediaSource.Factory(dataSourceFactory)
//                .createMediaSource(MediaItem.fromUri(url))
//            corePlayer.setMediaSource(hlsMediaSource)
            corePlayer.setMediaItem(MediaItem.fromUri(url))
            corePlayer.prepare()
            corePlayer.playWhenReady = true
        }
    }

    fun play() {
        workHandler.post() {
            corePlayer.play()
        }
    }

    fun pause() {
        workHandler.post {
            corePlayer.pause()
        }
    }

    fun isPlaying(): Boolean {
        return corePlayer.isPlaying
    }

    fun playOrPause() {
        workHandler.post {
            if (!corePlayer.isPlaying) {
                corePlayer.play()
            } else {
                corePlayer.pause()
            }
        }
    }

    fun setDataSource(path: String?, headers: Map<String?, String?>?) {
        // 设置dataSource
        if (path == null || path.isEmpty()) {
            return
        }
        workHandler.post {
            mediaSource = mMediaSourceHelper.getMediaSource(path, headers)

            mediaSource.addEventListener(Handler(), mediaSourceEventListener)
            corePlayer.setMediaSource(mediaSource)
        }
    }

    fun prepare() {
        workHandler.post {
            corePlayer.prepare()
            corePlayer.playWhenReady = true
        }
    }
}